package com.sh.sales.happysales.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sh.polymedicine.happysale.result.SimpleResult;
import com.sh.polymedicine.happysale.result.UserInfoResult;
import com.sh.polymedicine.happysale.service.envelop.HappySaleManager;
import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.AppData;
import com.sh.sales.happysales.app.utils.LoginOutUtil;
import com.sh.sales.happysales.base.BaseActivity;
import com.sh.sales.happysales.datautils.LocalUserInfoUtils;
import com.sh.sales.happysales.service.LoginObsever;
import com.sh.sales.happysales.view.NewToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by zhouyucheng on 2017/9/14.
 */

public class PerfectInfoActivity extends BaseActivity implements LoginObsever {
    private static final String TAG = "PerfectInfoActivity";
    @BindView(R.id.profect_next)
    TextView profectNext;
    @BindView(R.id.profect_nick)
    EditText profectNick;
    @BindView(R.id.profect_location)
    TextView profectLocation;
    @BindView(R.id.lin_region)
    LinearLayout linRegion;
    @BindView(R.id.profect_name)
    EditText profectName;
    @BindView(R.id.phoneNum)
    TextView phoneNum;


    private String str_profectNick;
    private String str_profectLocation;
    private String regionId;
    private String str_profectName;


    private Subscriber<SimpleResult> subPerfectSubscriber;
    private UserInfoResult result;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perfectinfo);
        ButterKnife.bind(this);
        mAppManager.addActivity(this);
        mLoginObserable.registerObserver(this);
        result = AppData.getInstence().getmUserInfoResult();
        phoneNum.setText(result.getMobile());
    }


    @OnClick(R.id.profect_next)
    public void onViewClicked() {
        perfectUserInfo();
    }

    private void perfectUserInfo() {
        String userSign = AppData.getInstence().getmUserToken().getUserSign();
        str_profectNick = profectNick.getText().toString().trim();
        str_profectName = profectName.getText().toString().trim();
        if ("".equals(str_profectNick) || "".equals(regionId) || "".equals(str_profectName)) {
            NewToast.makeText(PerfectInfoActivity.this, getResources().getString(R.string.profect_notfull));
            return;
        }
        //提交进入下一步
        if (subPerfectSubscriber != null) {
            if (!subPerfectSubscriber.isUnsubscribed()) {
                subPerfectSubscriber.unsubscribe();
            }
        }
        subPerfectSubscriber = new Subscriber<SimpleResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError:" + e);
                if (HappySaleManager.DATA_FAIL.equals(e.getMessage())) {
                    String message = e.getCause().getCause().getMessage();
                    NewToast.makeText(PerfectInfoActivity.this, message);
                    int ret = Integer.valueOf(e.getCause().getMessage());
                    if (ret >= 100 && ret < 200) {
                        LoginOutUtil.logOut(PerfectInfoActivity.this);
                    }
                } else if (HappySaleManager.DATA_NULL.equals(e.getMessage())) {
                    NewToast.makeText(PerfectInfoActivity.this, e.getCause().getMessage());
                } else {
                    NewToast.makeText(PerfectInfoActivity.this, "个人信息提交失败，请检查网络");
                }
            }

            @Override
            public void onNext(SimpleResult mSimpleResult) {

                result.setRegion(regionId);
                result.setRealName(str_profectLocation);
                result.setNickname(str_profectNick);
                result.setRealName(str_profectName);
                LocalUserInfoUtils.updataUserInfoResult(result, result.getInfoId());

                Intent m = new Intent();
                m.setClass(PerfectInfoActivity.this, ContentActivity.class);
                startActivity(m);
                mAppManager.finishActivity(PerfectInfoActivity.this);
            }
        };

        mHappySaleManager.saveMyInfo(str_profectNick, regionId, str_profectName, userSign, subPerfectSubscriber).subscribe(subPerfectSubscriber);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 5:
                str_profectLocation = data.getStringExtra("change02");
                regionId = data.getStringExtra("change01");
                profectLocation.setText(str_profectLocation);
                break;
        }
    }

    @OnClick({R.id.lin_region})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lin_region:
                startActivityForResult(new Intent(PerfectInfoActivity.this, RegionActivity.class), 5);
                break;
        }
    }

    @Override
    public void refreshUserSign(int Position) {
        perfectUserInfo();
    }

    @Override
    protected void onDestroy() {
        mLoginObserable.unregisterObserver(this);
        super.onDestroy();
    }
}
